﻿using System;
using System.Web.Mvc;
using EditorialProyectos.Business;
using EditorialProyectos.Business.Entities;
using EditorialProyectos.Business.Entities.Enum;
using EditorialProyectos.Website.Infrastructure;
using EditorialProyectos.Website.Models;
using Whoever.Framework.CustomException;
using Whoever.Framework.Enum;
using Whoever.Framework.Utils;
using Whoever.Patterns.MVC.Filters;
using Whoever.Patterns.MVC.Models;

namespace EditorialProyectos.Website.Controllers
{
    public class OnRevisionProjectController : Controller
    {
        public ActionResult Main()
        {
            var model = new OnRevisionProjectModel();

            ManagerFactory.ProjectManager.FillSelectablesRelations(model);

            model.List = ManagerFactory.ProjectManager.GetOnRevision();

            return View(model);
        }

        [AJAXExceptionHandler]
        public ActionResult RebindList()
        {
            return Json(ManagerFactory.ProjectManager.GetOnRevision());
        }

        [AJAXExceptionHandler]
        public ActionResult GetHtml(int id)
        {
            return Json(ManagerFactory.ProjectManager.GetHtml(id));
        }

        [AJAXExceptionHandler]
        public ActionResult Update(Project entity)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var item = ManagerFactory.ProjectManager.GetById(entity);

                    if (TryUpdateModel(item))
                        ManagerFactory.ProjectManager.UpdateRevision(item, User.IsInRole("Admin"));

                    return Json(new AJAXResponse { Message = Localized.Message("Update", MessageType.Success), Success = true, MustRedirect = false });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        [AJAXExceptionHandler]
        public ActionResult BackToPending(Project entity)
        {
            try
            {
                if (User.IsInRole("Admin"))
                {
                    var item = ManagerFactory.ProjectManager.GetById(entity);

                    ManagerFactory.ProjectManager.BackToPending(item);

                    return Json(new AJAXResponse { Message = Localized.Message("Update", MessageType.Success), Success = true, MustRedirect = false });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }

        [AJAXExceptionHandler]
        public ActionResult SetToApproved(Project entity)
        {
            try
            {
                if (User.IsInRole("Admin"))
                {
                    var item = ManagerFactory.ProjectManager.GetById(entity);

                    ManagerFactory.ProjectManager.SetToApproved(item);

                    return Json(new AJAXResponse { Message = Localized.Message("Update", MessageType.Success), Success = true, MustRedirect = false });
                }
                throw new BusinessException(Localized.Message("Model", MessageType.Error));
            }
            catch (BusinessException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception(Localized.Message("Exception", MessageType.Error));
            }
        }
    }
}
